﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StoragePeriod.IDAL.FoodTesting;
using StoragePeriod.Model.FoodTesting;
using StoragePeriod.DBUtility;
using System.Data;
using System.Data.SqlClient;

namespace StoragePeriod.SqlServerDAL.FoodTesting
{
    /// <summary>
    /// 感官评分 接口
    /// </summary>
    public class D_ScoreData:I_ScoreData
    {
        /// <summary>
        /// 获取检查实体
        /// </summary>
        /// <param name="eScoreData">查询ID</param>
        /// <returns>返回对应的实体</returns>
        public List<E_ScoreData> GetDataEntityByTime(E_ScoreData eScoreData)
        {
            StringBuilder sbSql = new StringBuilder();
            sbSql.Append("select * from ScoreData where RawId=" + eScoreData.RawId + " and ScoreTime=cast('" + eScoreData.ScoreTime.ToString() + "' as datetime)");
            DataTable dt = DbHelperSQL.Query(sbSql.ToString()).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                List<E_ScoreData> EntityList = new ModelHandler<E_ScoreData>().FillModel(dt);
                return EntityList;
            }
            return null;
        }

        /// <summary>
        /// 根据原料ID和检测时间 删除检测记录
        /// </summary>
        /// <param name="eVcData">要删除的检测记录</param>
        /// <returns></returns>
        public int RemoveByTime(E_ScoreData eScoreData)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ScoreData where RawId=" + eScoreData.RawId + " and ScoreTime=CAST('" + eScoreData.ScoreTime.ToString() + "' as datetime)");
            return DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(E_ScoreData eScoreData)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ScoreData(");
            strSql.Append("RawId,ScoreTime,GradingId,Score)");
            strSql.Append(" values (");
            strSql.Append("@RawId,@ScoreTime,@GradingId,@Score)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@RawId", SqlDbType.Int,4),
					new SqlParameter("@ScoreTime", SqlDbType.DateTime),
					new SqlParameter("@GradingId", SqlDbType.Int,4),
					new SqlParameter("@Score", SqlDbType.Int,4)};
            parameters[0].Value = eScoreData.RawId;
            parameters[1].Value = eScoreData.ScoreTime;
            parameters[2].Value = eScoreData.GradingId;
            parameters[3].Value = eScoreData.Score;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
    }
}
